package com.fuli.app.controller;

import com.fuli.app.common.ActionResult;
import com.fuli.app.common.ErrorInfo;
import com.fuli.app.common.Pagination;
import com.fuli.app.dto.IntegrateRecordDTO;
import com.fuli.app.dto.query.IntegrateRecordQueryDTO;
import com.fuli.app.service.IntegrateRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags ="积分记录")
@RequestMapping("/integrateRecord")
public class IntegrateRecordController {
    @Autowired
    private IntegrateRecordService integrateRecordService;

    @ApiOperation(value = "添加积分记录",
            notes = " <b>必传字段：</b><br/>" +
                    "1.score 积分<br/> " +
                    "2.remark 备注")
    @PostMapping("/add")
    public ActionResult<String> add(@ApiParam("积分记录DTO") @RequestBody IntegrateRecordDTO dto) {
        List<ErrorInfo> errors = new ArrayList<>();
        if (StringUtils.isEmpty(dto.getScore()))
            errors.add(new ErrorInfo(0, "积分不能为空！"));
        if (StringUtils.isEmpty(dto.getRemark()))
            errors.add(new ErrorInfo(0, "备注不能为空！"));
        if (!errors.isEmpty())
            return new ActionResult<>(errors);
        return integrateRecordService.add(dto);
    }

    @ApiOperation("删除积分记录")
    @DeleteMapping(value = "/delete/{id}")
    public ActionResult<String> delete(@ApiParam("id") @PathVariable String id) {
        return integrateRecordService.delete(id);
    }

    @ApiOperation("批量删除积分记录")
    @DeleteMapping(value = "/batchDelete")
    public ActionResult<List<String>> batchDelete(@ApiParam("积分记录id数组") @RequestParam List<String> ids) {
        return integrateRecordService.batchDelete(ids);
    }

    @ApiOperation("获取积分记录")
    @GetMapping(value = "/get/{id}")
    public ActionResult<IntegrateRecordDTO> get(@ApiParam("id") @PathVariable String id) {
        return integrateRecordService.get(id);
    }

    @ApiOperation("分页获取积分记录")
    @GetMapping(value = "/pageQuery")
    public ActionResult<Pagination<IntegrateRecordDTO>> listByQueryDTO(@ApiParam("积分记录查询DTO") @ModelAttribute IntegrateRecordQueryDTO QueryDTO) {
        return integrateRecordService.listByQueryDTO(QueryDTO);
    }


}
